package com.yfbao.horizon.schedule.tk.front2;

import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

public class TestJson {

    private static final ObjectMapper MAPPER = new ObjectMapper()
//            .registerModule(new AfterburnerModule())
            .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
            .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
            .setSerializationInclusion(JsonInclude.Include.NON_NULL);

    private static final ObjectWriter WRITER = MAPPER.writer();
    private static final Random random = new Random();
    private static final int batchSize = 5000;

    public static void main(String[] args) throws JsonProcessingException {
        long start = System.currentTimeMillis();
        for(int i=0;i<20;i++){
            System.out.println("batch:"+i);
            List<Front2LogEntry> list = getList();
            for (Front2LogEntry front2LogEntry : list) {
//                1、60529ms 2、44707ms
                JSONUtil.toJsonStr(front2LogEntry);
//               1、24831ms 2、24389ms
//                WRITER.writeValueAsString(front2LogEntry);
            }
        }
        System.out.println("cost:"+(System.currentTimeMillis()-start)+"ms");
    }


    private static List<Front2LogEntry> getList(){
        List<Front2LogEntry> batch = new ArrayList<>(batchSize);
        long now = System.currentTimeMillis();
        for (int i = 0; i < batchSize; i++) {
            long start = now + random.nextInt(50);
            long end = start + random.nextInt(500);
            Front2LogEntry e = new Front2LogEntry();
            e.setId(UUID.randomUUID().toString());
            e.setName(pickEventName());
            e.setType(pickEventType());
            e.setLevel(pickLevel()); // 可选
            e.setStartTime(start);
            e.setEndTime(end);
            e.setDuration(end - start);
            // data: 10-10000 随机长度；params: 10-5000 随机长度
            e.setData(randomString(10, 10000));
            e.setParams(randomString(10, 5000));
            batch.add(e);
        }
        return batch;
    }

    private static String pickLevel() {
        switch (random.nextInt(5)) {
            case 0: return "TRACE";
            case 1: return "DEBUG";
            case 2: return "INFO";
            case 3: return "WARN";
            default: return "ERROR";
        }
    }

    private static String pickEventName() {
        String[] names = {"click", "view", "xhr", "route", "download", "error"};
        return names[random.nextInt(names.length)];
    }

    private static String pickEventType() {
        String[] types = {"ui", "perf", "network", "biz"};
        return types[random.nextInt(types.length)];
    }

    // 生成指定范围的随机字符串（字母数字）
    private static String randomString(int minLen, int maxLen) {
        int len = minLen + random.nextInt(maxLen - minLen + 1);
        char[] alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            sb.append(alphabet[random.nextInt(alphabet.length)]);
        }
        return sb.toString();
    }
}
